<template>
  <div class="box12">
    <!-- 引入下拉框框架 -->
    <div class="box11">
      <div class="left" ref="left">
        <div>
          <div
            @click="changec(item.name)"
            :class="tas == item.name ? 'active' : ''"
            v-for="(item, i) in goodsList"
            :key="i"
          >
            <div>{{ item.name }}</div>
          </div>
        </div>
      </div>

      <div class="right" ref="right">
        <div>
          <div
            :id="item.name"
            class="tbox1"
            v-for="(item, i) in goodsList"
            :key="i"
          >
            <div class="st4">{{ item.name }}</div>

            <div
              @click="tcchange(value)"
              v-for="(value, j) in item.foods"
              :key="j"
            >
              <div class="box">
                <img :src="value.imgUrl" alt="" />
                <div style="width: 100%">
                  <h2>{{ value.name }}</h2>
                  <div class="st0">{{ value.goodsDesc }}</div>
                  <div class="st2">月售{{ value.sellCount }}好评率100%</div>
                  <div class="box44">
                    <div class="st3">￥{{ value.price }}</div>
                    <div class="btn">
                      <!-- 设置点击事件 传递它的id和改变的数量 -->
                      <button
                        v-if="value.num < 1 ? false : true"
                        @click.stop="jianshao(value.id, -1)"
                      >
                        -
                      </button>
                      <input
                        v-if="value.num < 1 ? false : true"
                        type="text"
                        :value="value.num"
                      />
                      <button @click.stop="jianshao(value.id, +1)">+</button>
                    </div>
                  </div>
                  <!-- 加减数量 -->
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 弹窗 列表详情 -->
      <div v-if="isshow2" class="tcbox">
        <div class="pos1"><img :src="tanchuang.imgUrl" alt="" /></div>
        <div class="pos2">
          <div class="pos2-1">{{ tanchuang.name }}</div>
          <div class="pos2-2">
            月售{{ tanchuang.sellCount }}好评率{{ tanchuang.rating }}
          </div>

          <div class="pos-3">
            <div class="pos-3-1">￥ {{ tanchuang.sellCount }}</div>
            <div>
              <van-button class="pos-3-2" round type="info"
                >加入购物车</van-button
              >
            </div>
          </div>
        </div>
        <div class="gongju"></div>
        <div class="pos4">
          <h3>商品介绍</h3>
          <p>{{ tanchuang.goodsDesc }}</p>
        </div>
        <div class="gongju"></div>
        <div class="pos5">
          <h3>商品评价</h3>
          <div>
            <div class="pos5-1">
              <van-button @click="name = 'all'" type="primary"
                >全部{{ tanchuang.ratings.length }}
              </van-button>
              <van-button @click="name = 'zan'" type="primary"
                >推荐{{ zan }}</van-button
              >
              <van-button @click="name = 'nozan'" type="primary"
                >吐槽{{ nozan }}</van-button
              >
            </div>
            <van-divider />
            <div class="disp">
              <div
                @click="
                  bg == 'background:#ccc;'
                    ? (bg = 'background:blue;')
                    : (bg = 'background:#ccc;')
                "
                :style="bg"
                class="disp2"
              >
                <van-icon name="success" />
              </div>
              <div>只有内容的评价</div>
            </div>
            <van-divider />
            <div
              class="user-pinlun"
              v-for="(item, i) in filers"
              :key="i"
            >
              <div class="user">
                <div>{{ item.rateTime }}</div>
                <div class="user1">
                  {{ item.username }}
                  <img :src="item.avatar" alt="图片加载失败" />
                </div>
              </div>
              <div class="pinglun">
                <van-icon
                  style="margin-right: 5px"
                  color="blue"
                  name="good-job-o"
                />{{ item.text }}
              </div>
            </div>
          </div>
        </div>
        <div @click="isshow2 = false" class="returen1">返回</div>
      </div>
    </div>
  </div>
</template>

<script>
import { api_getshop } from "@/apis/api.js";

import BScroll from "better-scroll";
export default {
  data() {
    return {
      tas: "新品专享",
      isshow: false,
      isshow2: false,
      //来一个对象来保存弹窗的数据
      tanchuang: {},
      //定义一个样式
      bg: "background:#ccc;",
      //定义一个数组来保存它的类名
      name: "all",
    };
  },
  //搞个计算属性，计算属性会自动执行它的函数 类似 data
  computed: {
    goodsList() {
      return this.$store.state.getlist;
    },
    //在这里筛选出好评的数据长度
    zan() {
      return this.tanchuang.ratings.filter((item) => item.rateType == 0).length;
    },
    //在这里筛选出吐槽的数据长度
    nozan() {
      return this.tanchuang.ratings.filter((item) => item.rateType == 1).length;
    },
    //在这里判断name属性
    filers() {
      switch (this.name) {
        case "all":
          return this.tanchuang.ratings;
        case "zan":
          return this.tanchuang.ratings.filter((item) => item.rateType == 0);
        case "nozan":
          return this.tanchuang.ratings.filter((item) => item.rateType == 1);
      }
    },
    //筛选有字的内容
    scuan(){
      return this.tanchuang.ratings.filers(item=>item.text)
    }
  },

  async created() {
    let { goodsList } = await api_getshop();
    //一进来就给数组添加一个num属性  方便后面购物车加减
    goodsList.forEach((item) => {
      item.foods.forEach((val) => {
        val.num = 0;
      });
    });
    //第一步  先把数据发送给vuex动态机器
    this.$store.commit("CHANGESHUJU", goodsList);

    this.items = goodsList.map((item) => {
      return { text: item.name };
    });
    //执行滚动效果
    this.$nextTick(() => {
      let bsss = new BScroll(".left", {
        click: true,
      });
      this.bss = new BScroll(this.$refs.right, {
        //允许滚动事件
        probeType: 3,
        click: true,
      });

      //通过on来监听滚动的位置
      this.bss.on("scroll", (pro) => {
        let y = Math.abs(pro.y);
        //通过便利的方式来找到数组的所有值 并且赋值
        this.newarr.forEach((item) => {
          //判断值的区间  确定在那个范围之内触发滚动事件
          if (y >= item.min && y < item.max) {
            this.tas = item.title;
          }
        });
      });

      //设定公共的函数 主要想找出一个我们想要的数组
      let satr = 0;
      this.newarr = [];
      goodsList.forEach((item) => {
        //获取组件到顶部的距离
        this.h = document.getElementById(item.name).offsetHeight;

        //获取想要的的
        this.newarr.push({ min: satr, max: satr + this.h, title: item.name });
        satr = satr + this.h;
      });
    });
  },

  methods: {
    changec(qbs) {
      this.tas = qbs;
      //点击切换对应的页面  思路是点击这个按钮切换到对应的组件
      this.bss.scrollToElement(document.getElementById(qbs), 1000);
    },
    tcchange(tc) {
      this.tanchuang = tc;
      this.isshow2 = true;
      console.log(tc);
    },

    //这是点击购物车的数量的改变传递的函数
    jianshao(id, num) {
      //拿到它的值以后就传给vuex来改变它的值
      this.$store.commit("CHANGECLICK", { id, num });
    },
    //这是点击渲染出所有的有评论的数据
  },
};
</script>

<style lang="less" scoped>
.box {
  display: flex;
  height: 100px;
  width: 100%;
  padding: 6px 10px 10px 10px;
  box-sizing: border-box;
  align-items: center;
  img {
    width: 70px;
    height: 70px;
    justify-content: center;
    align-items: center;
    margin-right: 4px;
  }
}
.box1 {
  position: relative;
  div {
    padding-top: 6px;
    box-sizing: border-box;
  }
}
h2 {
  box-sizing: border-box;
  font-weight: bold;
  font-size: 14px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 190px;
}
.st1 {
  font-size: 12px;
  color: #a3a6af;
  padding-top: 6px;
  box-sizing: border-box;
}
.st0 {
  font-size: 12px;
  color: #a3a6af;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: 190px;
  padding-top: 6px;
  box-sizing: border-box;
}
.st2 {
  font-size: 12px;
  color: #a3a6af;
  padding-top: 6px;
  box-sizing: border-box;
}
.st3 {
  font-size: 16px;
  color: red;
  padding-top: 6px;
  box-sizing: border-box;
}
.st4 {
  text-indent: 6px;
  line-height: 20px;
  background: #f4f5f7;
  border: 1px solid #e1e3e7;
  color: #a8acb0;
}

.left {
  width: 20%;
  height: 100%;
  overflow-y: scroll;
  background: #f4f5f7;
  color: #0c1117;
  div {
    div {
      height: 50px;
      display: flex;
      align-items: center;
      text-align: center;
    }
  }
  .active {
    background: #ffffff;
  }
}
.right {
  width: 80%;
  height: 100%;
  overflow-y: auto;
}
.box11 {
  display: flex;
  height: 100%;
  width: 100%;
}
.box44 {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.btn {
  display: flex;
  height: 26px;
  input {
    width: 25px;
    height: 20px;
    text-align: center;
    margin: 0px 10px;
  }

  button {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 22px;
    width: 22px;
    list-style: none;
    border-radius: 12px;
    border: 1px solid #cccccc;
    text-align: center;
  }
}

.box12 {
  height: 100%;
  width: 100%;
}

.tcbox {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: scroll;
  bottom: 60px;
  left: 0;
  background: #ffffff;
  .returen1 {
    position: fixed;
    height: 26px;
    width: 40px;
    background: #ccc;
    color: #ffffff;
    top: 15px;
    left: 20px;
    text-align: center;
    line-height: 26px;
    border-radius: 6px;
  }
  .gongju {
    height: 30px;
    width: 100%;
    background: #eeeeee;
    border-bottom: 1px solid #cccccc;
    border-top: 1px solid #cccccc;
  }
  .pos1 {
    height: 350px;
    width: 98%;
    margin: 1% auto;
    img {
      height: 100%;
      width: 100%;
    }
  }
  .pos2 {
    padding: 20px;
    box-sizing: border-box;
    .pos2-1 {
      font-size: 16px;
      font-weight: 600;
    }
    .pos2-2 {
      font-size: 14px;
      color: #d6d7dc;
      padding: 20px 0px;
      box-sizing: border-box;
    }
    .pos-3 {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 1 0px 0px;
      box-sizing: border-box;
      .pos-3-1 {
        color: red;
        font-size: 16px;
      }
      .pos-3-2 {
        height: 26px;
      }
    }
  }
  .pos4 {
    padding: 20px;
    box-sizing: border-box;
    p {
      line-height: 25px;
      letter-spacing: 1px;
    }
  }
  .pos5 {
    width: 100%;
    padding: 20px;
    box-sizing: border-box;
    .pos5-1 {
      display: flex;
      .box23 {
        margin: 0 8px;
      }
    }
    .disp {
      display: flex;
      align-items: center;

      margin: 15px 0px;
      .disp2 {
        position: relative;
        height: 20px;
        width: 20px;
        border-radius: 50%;
        background: #ddd;
        border: 1px solid #cccccc;
        display: block;
        i {
          position: absolute;
          left: 0;
          top: 0;
          transform: translate(-16%, -10%);
          color: #ffffff;
          padding: 5px;
          box-sizing: border-box;
        }
      }
    }
    h3 {
      padding: 20px 0px;
      box-sizing: border-box;
      width: 100%;
    }
  }
}
.user {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #c3c5c8;
  .user1 {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  img {
    height: 20px;
    width: 20px;
    border-radius: 50%;
    margin-left: 8px;
  }
}
.pinglun {
  font-weight: bold;
  font-size: 14px;
}
.user-pinlun {
  padding: 20px;
  box-sizing: border-box;
}
</style>